package com.topisv.tms.web.focus;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.topisv.tms.entity.CusStockMerge;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.JSONParam;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.entity.stock.ViewCusStock;
import com.topisv.tms.service.CusStockMergeService;
import com.topisv.tms.service.CustomerAreaGroupService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.EdiGoodsSkuService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.stock.ViewCusStockService;
import com.topisv.tms.utils.vo.RestCustomersAreagroupVo;
import com.topisv.tms.web.base.BaseDataTableController;

@Controller
@RequestMapping(value = "/focusProductManager")
public class FocusProductManagerController extends BaseDataTableController{
    private static final Logger log = LoggerFactory.getLogger(FocusProductManagerController.class);
	
	private final String url="work/gather/product";
	
	@Autowired
	private CustomerAreaGroupService customerAreaGroupService;
	@Autowired
	private EdiGoodsSkuService ediGoodsSkuService;
	@Autowired
	private ViewCusStockService viewCusStockService;
	@Autowired
	private CustomersService customersService;
	@Autowired
	private CusStockMergeService  cusStockMergeService;
	
	@RequestMapping(value = "init")
	public String init(Model model) {
		_init(null,model);
		return url;
	}
	
	private String _init(String customerCode,Model model){
		ShiroUser user =getShiroUser();
		RestCustomersAreagroupVo restCustomersAreagroupVo=null;
		List<Customers> customers=customersService.loadCustomerByAuthority(user);
		try {
			if(customerCode==null){
				restCustomersAreagroupVo = customerAreaGroupService.getRestCustomersAreagroupVo(customers.get(0).getCustomerCode());// 设置网点信息
			}else{
				restCustomersAreagroupVo = customerAreaGroupService.getRestCustomersAreagroupVo(customerCode);// 设置网点信息
				model.addAttribute("customerCode", customerCode);
			}
		 } catch (Exception e) {
				log.error("/focusProductManager/init" + e);
				
		}
		model.addAttribute("customers", customers);
		model.addAttribute("object", restCustomersAreagroupVo);
		return customers.get(0).getCustomerCode();
	}
	
	@RequestMapping(value = "v2_show")
	public String list(Model model) {
		fillSelectValue(model, null);
		return url;
	}
	
	@RequestMapping(value = "page",method = RequestMethod.POST)
	public void page(@RequestBody JSONParam[] paramJson, HttpServletRequest request,HttpServletResponse response){
		try {
			// 客户端参数
			QueryParamHolder paramHolder = collectParam(paramJson);
			Map<String, Object> param = paramHolder.getSearchParams();
			Map<String, String> paramMap = paramHolder.getParamMap();
			String customerCode = (String) param.get("EQ_customer.customerCode");
			String warehouseCode = (String) param.get("EQ_warehouseCode");
			if (StringUtils.isBlank(customerCode)) {
				String customerCodes = this.customersService.loadCustomerByAuthority(getShiroUser().jobuuid);
				paramMap.put("customerCodes", StringUtils.isBlank(customerCodes) ? "blank" : customerCodes);
			} else {
				paramMap.put("customerCodes", customerCode);
			}
			if (StringUtils.isBlank(warehouseCode)) {
				paramMap.put("areagroups", getShiroUser().areagroups);
			} else {
				paramMap.put("areagroups", warehouseCode);
			}
			Page<ViewCusStock> page = viewCusStockService.loadViewStock(paramHolder);
			List<ViewCusStock> lists = page.getContent();
			ediGoodsSkuService.setViewStocks((String) paramHolder.getSearchParams().get("EQ_customer.customerCode"), lists);
			for (ViewCusStock list : lists) {
				CusStockMerge cusStockMerge = this.cusStockMergeService.findCustomerWarSku(list.getCustomer().getCustomerCode(), list.getWarehouseCode(), list.getSku());
				if (cusStockMerge != null) {
					list.setSalesNum(list.getSalesNum() - cusStockMerge.getOrderNum());
				}
			}
			// 封装查询结果
			response.getWriter().print(bulidJSON(page));
		} catch (Exception e) {
			
		}
	}
	
}
